!pip install mat73
import numpy as np
import matplotlib.pyplot as plt
import cv2
import mat73
barbara = mat73.loadmat('../data/barbara.mat')['imageOrig']/255.0
data_2 = cv2.imread('../data/grass.png')
data_3 = cv2.imread('../data/honeyCombReal.png')
grass = cv2.cvtColor(data_2, cv2.COLOR_BGR2GRAY)/255.0
honey = cv2.cvtColor(data_3, cv2.COLOR_BGR2GRAY)/255.0
plt.figure(figsize=(24,6))
plt.subplot(131)
plt.imshow(barbara, cmap='gray')
plt.colorbar()
plt.subplot(132)
plt.imshow(grass, cmap='gray')
plt.colorbar()
plt.subplot(133)
plt.imshow(honey, cmap='gray')
plt.colorbar()
def myGaussianNoise(image,sd):
noise = np.random.normal(0,sd,image.shape)
noisy_image = image + noise
noisy_image = (noisy_image-np.min(noisy_image))/(np.max(noisy_image)-np.min(noisy_image))
return noisy_image
corrupted_barbara = myGaussianNoise(barbara,0.05)
corrupted_grass = myGaussianNoise(grass,0.05)
corrupted_honey = myGaussianNoise(honey,0.05)
plt.figure(figsize=(24,6))
plt.subplot(131)
plt.imshow(corrupted_barbara, cmap='gray')
plt.colorbar()
plt.subplot(132)
plt.imshow(corrupted_grass, cmap='gray')
plt.colorbar()
plt.subplot(133)
plt.imshow(corrupted_honey, cmap='gray')
plt.colorbar()
def myLinearContrastStretching(img):
low = np.min(img)
high = np.max(img)
new_img = (img-low)/(high-low)
return new_img
def rmsd(img1,img2):
return np.linalg.norm(img1-img2)/(img1.shape[0])
def myBilateralFiltering(img,sigma_d,sigma_I):
f_dim = 7 # dimension of filter
img = np.pad(img, pad_width=f_dim//2, mode='constant', constant_values=0)
l, w = img.shape
# coefficients due to euclidian distance (7 x 7 neighbour)
x_, y_ = np.meshgrid(np.linspace(-1,1,f_dim), np.linspace(-1,1,f_dim))
d = np.sqrt(x_*x_+y_*y_)
mu_d = 0.0 # space parameters
mu_I = 0.0 # intensity parameters
gauss_sp = np.exp(-((d-mu_d)**2 / (2.0*sigma_d**2))) # gaussian distribution
# convolving with the filter
pad = f_dim//2
output = np.zeros((l-2*pad, w-2*pad), dtype="float32")
for y in np.arange(pad, l - pad):
for x in np.arange(pad, w - pad):
# region of interest from the input image in each iteration
roi = img[y - pad:y + pad + 1, x - pad:x + pad + 1]
# intensity difference with this pixel's in the region (7 x 7 neighbour)
ints = roi - img[y][x]
# point by point multiplication of space coefficients with intensity
# coefficients after computing later from the gaussian distribution
gauss_ints = np.exp(-((ints-mu_I)**2 / (2.0*sigma_I**2))) * gauss_sp
# result gauss_ints corresponds to the bilateral filter coefficients
# multiply the filter values pointwise in region of interest and add
mul = (roi * gauss_ints).sum()
output[y - pad, x - pad] = mul
return myLinearContrastStretching(output) # float [0, 1]
sigma_d1 = 0.186
sigma_i1 = 40
sigma_d2 = 0.162
sigma_i2 = 0.178
sigma_d3 = 0.155
sigma_i3 = 0.203
recovered_barbara = myBilateralFiltering(corrupted_barbara,sigma_d1,sigma_i1)
recovered_grass = myBilateralFiltering(corrupted_grass,sigma_d2,sigma_i2)
recovered_honey = myBilateralFiltering(corrupted_honey,sigma_d3,sigma_i3)
print("RMSD (optimal) :",rmsd(barbara,recovered_barbara),rmsd(grass,recovered_grass),rmsd(honey,recovered_honey))
plt.figure(figsize=(24,6))
plt.subplot(131)
plt.imshow(recovered_barbara, cmap='gray')
plt.colorbar()
plt.subplot(132)
plt.imshow(recovered_grass, cmap='gray')
plt.colorbar()
plt.subplot(133)
plt.imshow(recovered_honey, cmap='gray')
plt.colorbar()
sigma_d for barbara = 0.186
sigma_i for barbara = 40(it goes on decreasing as this is increased)
sigma_d for grass = 0.162
sigma_i for grass = 0.178
sigma_d for honey = 0.155
sigma_i for honey = 0.203 (optimal values)
Changes when gaussian noise is Changed
plt.figure(figsize=(24,6))
plt.subplot(131)
plt.imshow(barbara, cmap='gray')
plt.colorbar()
plt.subplot(132)
plt.imshow(corrupted_barbara, cmap='gray')
plt.colorbar()
plt.subplot(133)
plt.imshow(recovered_barbara, cmap='gray')
plt.colorbar()
plt.figure(figsize=(24,6))
plt.subplot(131)
plt.imshow(grass, cmap='gray')
plt.colorbar()
plt.subplot(132)
plt.imshow(corrupted_grass, cmap='gray')
plt.colorbar()
plt.subplot(133)
plt.imshow(recovered_grass, cmap='gray')
plt.colorbar()
plt.figure(figsize=(24,6))
plt.subplot(131)
plt.imshow(honey, cmap='gray')
plt.colorbar()
plt.subplot(132)
plt.imshow(corrupted_honey, cmap='gray')
plt.colorbar()
plt.subplot(133)
plt.imshow(recovered_honey, cmap='gray')
plt.colorbar()
Below is the spatial Gaussian filter for three images side by side(barbara,grass,honey)
plt.figure(figsize=(24,6))
plt.subplot(131)
plt.imshow(myLinearContrastStretching(barbara - recovered_barbara), cmap='gray')
plt.colorbar()
plt.subplot(132)
plt.imshow(myLinearContrastStretching(grass - recovered_grass), cmap='gray')
plt.colorbar()
plt.subplot(133)
plt.imshow(myLinearContrastStretching(honey - recovered_honey), cmap='gray')
plt.colorbar()
c_d = 0.9
c_i = 1
recovered_barbara = myBilateralFiltering(corrupted_barbara,c_d*sigma_d1,c_i*sigma_i1)
recovered_grass = myBilateralFiltering(corrupted_grass,c_d*sigma_d2,c_i*sigma_i2)
recovered_honey = myBilateralFiltering(corrupted_honey,c_d*sigma_d3,c_i*sigma_i3)
print("RMSD :",rmsd(barbara,recovered_barbara),rmsd(grass,recovered_grass),rmsd(honey,recovered_honey))
plt.figure(figsize=(24,6))
plt.subplot(131)
plt.imshow(recovered_barbara, cmap='gray')
plt.colorbar()
plt.subplot(132)
plt.imshow(recovered_grass, cmap='gray')
plt.colorbar()
plt.subplot(133)
plt.imshow(recovered_honey, cmap='gray')
plt.colorbar()
c_d = 1.1
c_i = 1
recovered_barbara = myBilateralFiltering(corrupted_barbara,c_d*sigma_d1,c_i*sigma_i1)
recovered_grass = myBilateralFiltering(corrupted_grass,c_d*sigma_d2,c_i*sigma_i2)
recovered_honey = myBilateralFiltering(corrupted_honey,c_d*sigma_d3,c_i*sigma_i3)
print("RMSD :",rmsd(barbara,recovered_barbara),rmsd(grass,recovered_grass),rmsd(honey,recovered_honey))
plt.figure(figsize=(24,6))
plt.subplot(131)
plt.imshow(recovered_barbara, cmap='gray')
plt.colorbar()
plt.subplot(132)
plt.imshow(recovered_grass, cmap='gray')
plt.colorbar()
plt.subplot(133)
plt.imshow(recovered_honey, cmap='gray')
plt.colorbar()
c_d = 1
c_i = 0.9
recovered_barbara = myBilateralFiltering(corrupted_barbara,c_d*sigma_d1,c_i*sigma_i1)
recovered_grass = myBilateralFiltering(corrupted_grass,c_d*sigma_d2,c_i*sigma_i2)
recovered_honey = myBilateralFiltering(corrupted_honey,c_d*sigma_d3,c_i*sigma_i3)
print("RMSD :",rmsd(barbara,recovered_barbara),rmsd(grass,recovered_grass),rmsd(honey,recovered_honey))
plt.figure(figsize=(24,6))
plt.subplot(131)
plt.imshow(recovered_barbara, cmap='gray')
plt.colorbar()
plt.subplot(132)
plt.imshow(recovered_grass, cmap='gray')
plt.colorbar()
plt.subplot(133)
plt.imshow(recovered_honey, cmap='gray')
plt.colorbar()
c_d = 1
c_i = 1.1
recovered_barbara = myBilateralFiltering(corrupted_barbara,c_d*sigma_d1,c_i*sigma_i1)
recovered_grass = myBilateralFiltering(corrupted_grass,c_d*sigma_d2,c_i*sigma_i2)
recovered_honey = myBilateralFiltering(corrupted_honey,c_d*sigma_d3,c_i*sigma_i3)
print("RMSD :",rmsd(barbara,recovered_barbara),rmsd(grass,recovered_grass),rmsd(honey,recovered_honey))
plt.figure(figsize=(24,6))
plt.subplot(131)
plt.imshow(recovered_barbara, cmap='gray')
plt.colorbar()
plt.subplot(132)
plt.imshow(recovered_grass, cmap='gray')
plt.colorbar()
plt.subplot(133)
plt.imshow(recovered_honey, cmap='gray')
plt.colorbar()